package mongodb.general.operator.mongoHelper.controller;

import mongodb.general.operator.config.Constant;
import mongodb.general.operator.config.base.ViewResult;
import mongodb.general.operator.mongoHelper.common.Page;
import mongodb.general.operator.mongoHelper.common.builder.SortBuilder;
import mongodb.general.operator.mongoHelper.common.dommin.Test;
import mongodb.general.operator.mongoHelper.db.MongoHelper;
import mongodb.general.operator.mongoHelper.wrapper.CriteriaAndWrapper;
import mongodb.general.operator.mongoHelper.wrapper.CriteriaOrWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
//@RequestMapping("")
public class TestController {

    @Autowired
    private MongoHelper mongoHelper;

    @GetMapping(value = "/page")
    public ViewResult<Page<Test>> page() {
        Page<Test> page = mongoHelper.findPage(new Page<Test>(), Test.class);
        return ViewResult.success(page);
    }

    @GetMapping(value = "/list")
    public ViewResult<List> list(String[] ids) {
        List<String> strings = Arrays.asList(ids);
        CriteriaAndWrapper criteriaAndWrapper2 =
                new CriteriaAndWrapper().in(Constant::getId, strings);
        SortBuilder sortBuilder2 = new SortBuilder().add(Constant::getId, Sort.Direction.DESC);
        CriteriaOrWrapper criteriaorWrapper = new CriteriaOrWrapper();
        criteriaorWrapper.eq(Constant::getId,"12");
        criteriaorWrapper.eq(Constant::getId,"32");
        criteriaAndWrapper2.and(criteriaorWrapper);

//        CriteriaAndWrapper criteriaAndWrapper = new CriteriaAndWrapper();
//        SortBuilder sortBuilder = new SortBuilder();
        List<Test> empty_collection = mongoHelper.findListByQuery(
                criteriaAndWrapper2, sortBuilder2,
                Test.class
        );
        List<Object> empty_collection2 = mongoHelper.findListByQuery(
                criteriaAndWrapper2, sortBuilder2,
                "empty_collection"
        );
        return ViewResult.success(empty_collection);
    }
}
